package com.open.capacity.workflow.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.commons.Result;
import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.PageUtil;
import com.open.capacity.utils.SysUserUtil;
import com.open.capacity.workflow.dao.WFSchemeAuthorizeDao;
import com.open.capacity.workflow.dao.WFSchemeDao;
import com.open.capacity.workflow.dao.WFSchemeinfoDao;
import com.open.capacity.workflow.model.WFScheme;
import com.open.capacity.workflow.model.WFSchemeAuthorize;
import com.open.capacity.workflow.model.WFSchemeinfo;
import com.open.capacity.workflow.service.WFSchemeService;
import com.open.capacity.workflow.vo.SchemeVo;
import com.open.capacity.workflow.vo.WorkFlowSchemeVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class WFSchemeServiceImpl implements WFSchemeService {

    @Autowired
    private WFSchemeinfoDao schemeinfoDao;

    @Autowired
    private WFSchemeDao schemeDao;

    @Autowired
    private WFSchemeAuthorizeDao schemeAuthorizeDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(WFSchemeinfo confluence) {
        schemeinfoDao.save(confluence);
        log.info("保存流程信息：{}", confluence);
    }

    @Override
    public Result updateStatus(String id, Integer status) {
        WFSchemeinfo schemeinfo = schemeinfoDao.findById(id);
        schemeinfoDao.updateStatus(id, status);
        log.info("修改状态：{}", schemeinfo);

        return Result.succeed("修改成功");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(WFSchemeinfo confluence) {

        schemeinfoDao.updateByOps(confluence);
        log.info("修改流程信息：{}", confluence);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        WFSchemeinfo schemeinfo = schemeinfoDao.findById(id);
        schemeinfoDao.delete(id);
        schemeDao.delete(id);

        log.info("删除流程信息：{}", schemeinfo);
    }


    @Override
    public SchemeVo findById(String id) {

        SchemeVo vo = new SchemeVo();
        WFSchemeinfo schemeinfo = schemeinfoDao.findById(id);
        WFScheme scheme = schemeDao.findById(schemeinfo.getSchemeId());
        vo.setScheme(scheme);
        vo.setSchemeinfo(schemeinfo);
        return vo;
    }

    @Override
    public WFScheme findSchemeBySchemeinfoId(String schemeinfoId) {
        return schemeDao.findBySchemeinfoId(schemeinfoId);
    }

    @Override
    public WFScheme findSchemeById(String id) {
        return schemeDao.findById(id);
    }

    @Override
    public WFSchemeinfo findSchemeinfoByCode(String companyId, String code) {
        return schemeinfoDao.findByCode(companyId, code);
    }

    @Override
    public List<WFSchemeinfo> findSchemeinfos(Map<String, Object> params) {
        List<WFSchemeinfo> list = schemeinfoDao.findList(params);
        return list;
    }


    @Override
    public PageResult<WFSchemeinfo> pageSchemeinfos(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<WFSchemeinfo> list = schemeinfoDao.findList(params);
        PageInfo<WFSchemeinfo> pageInfo = new PageInfo(list);

        return PageResult.<WFSchemeinfo>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result saveOrUpdate(WorkFlowSchemeVo schemeVo) {
        int i = 0;
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();
        if (StringUtils.isNotEmpty(schemeVo.getId())) {
            WFSchemeinfo schemeinfo = schemeinfoDao.findById(schemeVo.getId());
            String id = UUIDUtils.getOriginalUUIDWithoutBar();
            Date now = new Date();
            if (schemeinfo != null) {
                WFSchemeinfo newData = schemeVo.getSchemeinfo();
                schemeinfo.setCode(newData.getCode());
                schemeinfo.setName(newData.getName());
                schemeinfo.setCategory(newData.getCategory());
                schemeinfo.setKind(newData.getKind());
                schemeinfo.setType(schemeVo.getType());
                schemeinfo.setStatus(newData.getStatus() == null ? 1 : newData.getStatus());
                schemeinfo.setDescription(newData.getDescription());
                schemeinfo.setSchemeId(id);
                schemeinfo.setUpdateUserid(loginUser.getId());
                schemeinfo.setUpdateUsername(loginUser.getRealName());
                schemeinfo.setUpdateTime(now);
                i = schemeinfoDao.updateByOps(schemeinfo);
            }

            WFScheme scheme = new WFScheme();
            scheme.setId(id);
            scheme.setCompanyId(schemeVo.getCompanyId());
            scheme.setSchemeinfoId(schemeinfo.getId());
            scheme.setType(schemeVo.getType());
            scheme.setScheme(schemeVo.getScheme());
            scheme.setCreateUserid(loginUser.getId());
            scheme.setCreateUsername(loginUser.getRealName());
            scheme.setCreateTime(now);
            schemeDao.save(scheme);

            //  schemeAuthorize
            schemeAuthorizeDao.deleteBySchemeinfoId(schemeinfo.getId());
            List<WFSchemeAuthorize> schemeAuthorizes = schemeVo.getSchemeAuthorizes();
            for (WFSchemeAuthorize authorize : schemeAuthorizes) {
                authorize.setSchemeinfoId(schemeinfo.getId());
                schemeAuthorizeDao.save(authorize);
            }
        } else {
            Date now = new Date();
            String id = UUIDUtils.getOriginalUUIDWithoutBar();
            WFSchemeinfo schemeinfo = schemeVo.getSchemeinfo();
            List<WFSchemeAuthorize> schemeAuthorizes = schemeVo.getSchemeAuthorizes();

            schemeinfo.setId(id);
            schemeinfo.setCompanyId(schemeVo.getCompanyId());
            schemeinfo.setStatus(1);
            schemeinfo.setSchemeId(id);
            schemeinfo.setCreateUserid(loginUser.getId());
            schemeinfo.setCreateUsername(loginUser.getRealName());
            schemeinfo.setCreateTime(now);
            schemeinfo.setUpdateTime(now);
            schemeinfo.setType(schemeVo.getType());

            WFScheme scheme = new WFScheme();
            scheme.setId(id);
            scheme.setCompanyId(schemeVo.getCompanyId());
            scheme.setType(schemeVo.getType());
            scheme.setScheme(schemeVo.getScheme());
            scheme.setSchemeinfoId(id);
            scheme.setCreateUserid(loginUser.getId());
            scheme.setCreateUsername(loginUser.getRealName());
            scheme.setCreateTime(now);

            for (WFSchemeAuthorize authorize : schemeAuthorizes) {
                authorize.setId(UUIDUtils.getOriginalUUIDWithoutBar());
                authorize.setCompanyId(schemeVo.getCompanyId());
                authorize.setSchemeinfoId(id);
                schemeAuthorizeDao.save(authorize);
            }

            schemeDao.save(scheme);
            i = schemeinfoDao.save(schemeinfo);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }


}
